{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Preprocessing text \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import libraries "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Basic libs \n",
    "import pandas as pd\n",
    "import string\n",
    "\n",
    "import re\n",
    "\n",
    "# Custom functions\n",
    "from clean_text import clean_tweet"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Import files "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import CSV file \n",
    "\n",
    "df = pd.read_csv('/home/jae/hateasiancovid/processed_data/US_tweets.csv',\n",
    "                 parse_dates = ['date'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1394478, 16)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1,394,478 obs, 16 columns  \n",
    "\n",
    "df.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Clean text "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Copy\n",
    "df_clean = df.copy()\n",
    "\n",
    "# Clean\n",
    "df_clean['full_text'] = clean_tweet(df_clean['full_text'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>created_at</th>\n",
       "      <th>full_text</th>\n",
       "      <th>location</th>\n",
       "      <th>US</th>\n",
       "      <th>non_US</th>\n",
       "      <th>wuhan</th>\n",
       "      <th>asian</th>\n",
       "      <th>chinese</th>\n",
       "      <th>trump</th>\n",
       "      <th>date</th>\n",
       "      <th>anti_asian</th>\n",
       "      <th>chinese_virus</th>\n",
       "      <th>kung_flu</th>\n",
       "      <th>wuhan_virus</th>\n",
       "      <th>anti_racism</th>\n",
       "      <th>racism</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Fri Jan 31 19:33:48 +0000 2020</td>\n",
       "      <td>my step dad got pneumonia and said he got the ...</td>\n",
       "      <td>Los Angeles, CA</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2020-01-31</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Wed Jan 29 17:02:34 +0000 2020</td>\n",
       "      <td>coronavirus 133 dead out of 6091 infected  92...</td>\n",
       "      <td>San Jose, CA</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2020-01-29</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Fri Jan 31 14:21:47 +0000 2020</td>\n",
       "      <td>thank you  for helping to get information to d...</td>\n",
       "      <td>Delaware</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2020-01-31</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Wed Jan 29 21:57:23 +0000 2020</td>\n",
       "      <td>them asu students dirty af mfs got multiple s...</td>\n",
       "      <td>United States</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2020-01-29</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Tue Jan 28 14:00:27 +0000 2020</td>\n",
       "      <td>lord protect every one from the coronavirus</td>\n",
       "      <td>Washington, DC</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2020-01-28</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                       created_at  \\\n",
       "0  Fri Jan 31 19:33:48 +0000 2020   \n",
       "1  Wed Jan 29 17:02:34 +0000 2020   \n",
       "2  Fri Jan 31 14:21:47 +0000 2020   \n",
       "3  Wed Jan 29 21:57:23 +0000 2020   \n",
       "4  Tue Jan 28 14:00:27 +0000 2020   \n",
       "\n",
       "                                           full_text         location  US  \\\n",
       "0  my step dad got pneumonia and said he got the ...  Los Angeles, CA   1   \n",
       "1   coronavirus 133 dead out of 6091 infected  92...     San Jose, CA   1   \n",
       "2  thank you  for helping to get information to d...         Delaware   1   \n",
       "3   them asu students dirty af mfs got multiple s...    United States   1   \n",
       "4       lord protect every one from the coronavirus    Washington, DC   1   \n",
       "\n",
       "   non_US  wuhan  asian  chinese  trump       date  anti_asian  chinese_virus  \\\n",
       "0       0      0      0        0      0 2020-01-31           0              0   \n",
       "1       0      0      0        0      0 2020-01-29           0              0   \n",
       "2       0      0      0        0      0 2020-01-31           0              0   \n",
       "3       0      0      0        0      0 2020-01-29           0              0   \n",
       "4       0      0      0        0      0 2020-01-28           0              0   \n",
       "\n",
       "   kung_flu  wuhan_virus  anti_racism  racism  \n",
       "0         0            0            0       0  \n",
       "1         0            0            0       0  \n",
       "2         0            0            0       0  \n",
       "3         0            0            0       0  \n",
       "4         0            0            0       0  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check \n",
    "df_clean.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Export "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_clean = df_clean.drop(columns = ['created_at', 'location'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_clean.to_csv(\"/home/jae/hateasiancovid/processed_data/cleaned_data.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
